<?php

/**
 * This is the model class for table "comments".
 *
 * The followings are the available columns in table 'comments':
 * @property integer $id
 * @property integer $user_id
 * @property string $name
 * @property string $email
 * @property string $content
 * @property integer $parent_id
 * @property integer $post_id
 * @property integer $status
 * @property string $create_at
 *
 * The followings are the available model relations:
 * @property Announcement $post
 * @property Users $user
 * @property Comments $parent
 * @property Comments[] $comments
 */
class Comments extends CommentsBase
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Comments the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array_merge(parent::relations(),array(
			'reply' => array(self::HAS_MANY, 'Comments', 'parent_id'),
			'user' => array(self::BELONGS_TO, 'Account', 'user_id'),
		));
	}

	public function countComment($id) {
		return $this->countByAttributes(array('post_id' => $id, 'status' => '1'));
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('parent_id',$this->parent_id);
		$criteria->compare('post_id',$this->post_id);
		$criteria->compare('status',$this->status);
		$criteria->compare('create_at',$this->create_at,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}